package com.sicnu.cheer.leaugebar.leaguemodle.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gigamole.infinitecycleviewpager.HorizontalInfiniteCycleViewPager;
import com.sicnu.cheer.generalmodule.fragment.base.BaseFragment;
import com.sicnu.cheer.leaugebar.leaguemodle.R;
import com.sicnu.cheer.leaugebar.leaguemodle.activity.AlbumActivity;
import com.sicnu.cheer.leaugebar.leaguemodle.activity.CreateAlbumActivity;
import com.sicnu.cheer.leaugebar.leaguemodle.adapter.HorizontalPagerAdapter;
import com.sicnu.cheer.leaugebar.leaguemodle.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheer on 2017/2/22.
 * 照片集布局
 */

public class ImgFragment extends BaseFragment implements View.OnClickListener {
    private List<Utils.LibraryObject> list;
    private ImageView moreIV;
    public List<String> urls;
    private LinearLayout noAlbumLL;
    private Button createAlbumBtn;
    private HorizontalInfiniteCycleViewPager cycleViewPager;
    private TextView progressTip;
    private TextView tipTV;
    private int state;

    @Override
    protected int setLayoutResourceId() {
        return R.layout.fragment_img;
    }

    @Override
    protected void initData(Bundle arguments) {
        super.initData(arguments);
        this.list = new ArrayList<>();
    }

    @Override
    protected void initView() {
        super.initView();
        cycleViewPager = findViewById(R.id.hicvp);
        moreIV = findViewById(R.id.more_img_iv);
        moreIV.setOnClickListener(this);
        tipTV = findViewById(R.id.tip_tv);
        noAlbumLL = findViewById(R.id.no_album_ll);
        createAlbumBtn = findViewById(R.id.create_album_btn);
        createAlbumBtn.setOnClickListener(this);
        noAlbumLL.setVisibility(View.GONE);
        moreIV.setVisibility(View.GONE);
        noAlbumLL.setVisibility(View.GONE);

        //加载中的提示信息
        progressTip = findViewById(R.id.progress_tv);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    public void refreshLayout(int state) {
        this.state=state;
        progressTip.setVisibility(View.GONE);
        switch (state) {
            case -1:
                progressTip.setVisibility(View.VISIBLE);
                break;
            case 0:
                tipTV.setText("马上新建相册，与社团好友分享照片");
                createAlbumBtn.setText("新建相册");
                cycleViewPager.setVisibility(View.GONE);
                noAlbumLL.setVisibility(View.VISIBLE);
                moreIV.setVisibility(View.GONE);
                break;
            case 1:
                tipTV.setText("马上上传照片，收集我们回忆的点点滴滴");
                createAlbumBtn.setText("上传照片");
                cycleViewPager.setVisibility(View.GONE);
                noAlbumLL.setVisibility(View.VISIBLE);
                moreIV.setVisibility(View.GONE);
                break;
            case 2:
                cycleViewPager.setVisibility(View.VISIBLE);
                noAlbumLL.setVisibility(View.GONE);
                moreIV.setVisibility(View.VISIBLE);
                Utils.LibraryObject object;
                if (urls != null) {
                    for (int i = 0; i < urls.size(); i++) {
                        object = new Utils.LibraryObject(urls.get(i), urls.get(i));
                        this.list.add(object);
                        if (i >= 7) {
                            break;
                        }
                    }
                }
                cycleViewPager.setAdapter(new HorizontalPagerAdapter(getContext(), list));
                cycleViewPager.startAutoScroll(true);
                cycleViewPager.setScrollDuration(2000);
                break;
        }

//        if (urls == null||urls.size() == 0) {
//            cycleViewPager.setVisibility(View.GONE);
//            noAlbumLL.setVisibility(View.VISIBLE);
//            moreIV.setVisibility(View.GONE);
//        } else {
//            cycleViewPager.setVisibility(View.VISIBLE);
//            noAlbumLL.setVisibility(View.GONE);
//            moreIV.setVisibility(View.VISIBLE);
//            Utils.LibraryObject object;
//            if (urls != null) {
//                for (int i = 0; i < urls.size(); i++) {
//                    object = new Utils.LibraryObject(urls.get(i), urls.get(i));
//                    this.list.add(object);
//                    if (i >= 7) {
//                        break;
//                    }
//                }
//            }
//            cycleViewPager.setAdapter(new HorizontalPagerAdapter(getContext(), list));
//            cycleViewPager.startAutoScroll(true);
//            cycleViewPager.setScrollDuration(2000);
//        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.more_img_iv) {
            Intent intent = new Intent(mActivity, AlbumActivity.class);
            startActivityForResult(intent, 121);
        } else if (v.getId() == R.id.create_album_btn) {
            Intent intent = new Intent();
            switch (state){
                case 0:
                    intent.setClass(mActivity, CreateAlbumActivity.class);
                    break;
                case 1:
                    intent.setClass(mActivity, AlbumActivity.class);
                    break;
            }
            startActivityForResult(intent, 121);
        }
    }
}
